package com.ksd.modules.course.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.annotation.ApiLog;
import com.ksd.common.controller.BaseController;
import com.ksd.common.core.result.R;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.annotation.RepeatSubmit;
import com.ksd.modules.course.po.AdminCourseUserBuySavePO;
import com.ksd.modules.course.po.AdminCourseUserBuySearchPO;
import com.ksd.modules.course.service.IAdminCourseUserBuyService;
import com.ksd.modules.course.vo.AdminCourseUserBuyVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
public class AdminCourseUserBuyController extends BaseController {

    @Autowired
    private IAdminCourseUserBuyService adminCourseUserBuyService;

    /**
     * 【课程】-【订单】- 列表
     * @return
     */
    @PostMapping("/course/order/list")
    public R<Page<AdminCourseUserBuyVo>> findAdminCourses(@Validated @RequestBody AdminCourseUserBuySearchPO adminCourseCategorySearchPO){
        return R.data(adminCourseUserBuyService.findAdminCourseUserBuys(adminCourseCategorySearchPO));
    }


    /**
     * 【课程】-【订单】- 保存和更新
     * @param adminCourseCategorySavePO
     * @return
     */
    @PostMapping("/course/order/saveupdate")
    @ApiLog("【课程】-【订单】- 保存和更新")
    @RepeatSubmit
    public R<AdminCourseUserBuyVo> saveUpdateAdminCourse(@Validated @RequestBody AdminCourseUserBuySavePO adminCourseCategorySavePO){
        return R.data(adminCourseUserBuyService.saveUpdateAdminCourseUserBuy(adminCourseCategorySavePO));
    }

    /**
     * 【课程】-【订单】- 根据id查询明细
     * @param id
     * @return
     */
    @PostMapping("/course/order/get/{id}")
    public R<AdminCourseUserBuyVo> getAdminCourseById(@PathVariable("id")Long id){
        return R.data(adminCourseUserBuyService.getAdminCourseUserBuyById(id));
    }

    /**
     * 【课程】-【订单】- 批量彻底删除
     *
     * @return
     */
    @PostMapping("/course/order/batch/remove")
    @ApiLog("【课程】-【订单】- 批量物理删除")
    @RepeatSubmit
    public R<Boolean> batchRemovesAdminCourse(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminCourseUserBuyService.batchRemovesAdminCourseUserBuy(batchIdsPO));
    }

    /**
     * 【课程】-【订单】- 单个物理删除
     * @param id
     * @return
     */
    @PostMapping("/course/order/remove/{id}")
    @ApiLog("【课程】-【订单】- 单个物理删除")
    public R<Boolean> removeAdminCourseById(@PathVariable("id")Long id){
        return R.data(adminCourseUserBuyService.removeAdminCourseUserBuyById(id));
    }

    /**
     * 【课程】-【订单】- 修改状态
     *
     * @return
     */
    @PostMapping("/course/order/status/{id}/{field}")
    @ApiLog("【课程】-【订单】- 修改状态")
    @RepeatSubmit
    public R<Boolean> updateStatus(
            @PathVariable("id") Long id,
            @PathVariable("field") String field,
            @RequestParam("value") String value
    ) {
        if(StringUtils.isNumeric(value)) {
            return R.data(adminCourseUserBuyService.updateStatus(id, field, Integer.parseInt(value)));
        }else{
            return R.data(adminCourseUserBuyService.updateStatusStr(id, field, String.valueOf(value)));
        }
    }

    /**
     * 【课程】-【订单】- 绑定订单
     *
     * @return
     */
    @PostMapping("/course/order/userbind")
    @ApiLog("【课程】-【订单】- 绑定订单")
    @RepeatSubmit
    public R<Boolean> bindUserOrder(
            @RequestParam("uuid")String uuid,
            @RequestParam("opid") Long opid
    ) {
        return R.data(adminCourseUserBuyService.bindUserOrder(uuid, opid));
    }
}
